package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ProjectCompanyDTO;
import com.financing.platform.service.IProjectCompanyService;
import com.financing.platform.util.DTOdateSet;

/**
 * 
 * @ClassName: ProjectCompanyAction
 * @Description: 产品公司
 * @author lisu
 *
 */
@Controller("projectCompanyActionAPI")
@RequestMapping("/api/projectCompany")
@Scope("prototype")
public class ProjectCompanyAction extends BaseAction {

	@Autowired
	private IProjectCompanyService projectCompanyServiceImpl;

	@RequestMapping("/addProjectCompany.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response, ProjectCompanyDTO projectCompanyDTO) {
		try {
			log.info("In ProjectCompanyAction add begin!");

			// 初始化数据赋值
			DTOdateSet.setCreateInfo(projectCompanyDTO, request.getSession());
			DTOdateSet.setUpdateInfo(projectCompanyDTO, request.getSession());

			projectCompanyServiceImpl.addProjectCompany(projectCompanyDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("ProjectCompanyAction add error !", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setMessage(e.getCause().getMessage());
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		log.info("In ProjectCompanyAction add end!");
		return resultMap;
	}
	/**
	* 
	* @Title: update 
	* @Description: 修改产品公司
	* @param   request
	* @param   response
	* @param   projectCompanyDTO
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/updateProjectCompany.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request,
			HttpServletResponse response,ProjectCompanyDTO projectCompanyDTO,Pager pager) {
		
		log.info("In ProjectCompanyAction update begin!");
		
		try{
			if(projectCompanyDTO.getId() == null){
				resultMap.setStatus(STATUS.FAILE);
				log.info("In ProjectCompanyAction deleteCompany projectCompanyDTO id is null!");
				return resultMap;
			}
			DTOdateSet.setUpdateInfo(projectCompanyDTO, request.getSession());
			projectCompanyServiceImpl.updateProjectCompany(projectCompanyDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("AdvertAction updateAdvert error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In AdvertAction updateAdvert end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: 查询公司列表
	* @param   request
	* @param   response
	* @param   projectCompanyDTO
	* @return ResultMap   
	* @throws
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/listProjectCompany.shtml")
	@ResponseBody
	public ResultMap query(HttpServletRequest request,
			HttpServletResponse response,ProjectCompanyDTO projectCompanyDTO,Pager pager) {
		
		log.info("In ProjectCompanyAction querylist begin!");
		
		try{
			
			projectCompanyDTO.setBegin(pager.getStartIndex());
			projectCompanyDTO.setPageSize(pager.getPageSize());
			
			
			int count = projectCompanyServiceImpl.getProjectCompanyListCount(projectCompanyDTO);
			
			pager.setTotalCount(count);
			
			List<ProjectCompanyDTO> list;
			if(count>0){
				list = projectCompanyServiceImpl.getProjectCompanyList(projectCompanyDTO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager",pager);
		}catch(Exception e){
			log.error("ProjectCompanyAction querylist error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In ProjectCompanyAction querylist end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: queryCompany 
	* @Description: 查询公司详情
	* @param   request
	* @param   response
	* @param   advertId
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/queryProjectCompanyView.shtml")
	@ResponseBody
	public ResultMap queryCompany(HttpServletRequest request,
			HttpServletResponse response,Integer companyId) {
		
		log.info("In ProjectCompanyAction queryCompany Begin!");
		
		try{
			ProjectCompanyDTO dto = projectCompanyServiceImpl.getProjectCompanyById(companyId);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("dto", dto);
		}catch(Exception e){
			log.error("ProjectCompanyAction queryCompany error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In ProjectCompanyAction queryCompany end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title:  deleteCompany
	* @Description: 查询公司详情
	* @param   request
	* @param   response
	* @param   advertId
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/deleteProjectCompany.shtml")
	@ResponseBody
	public ResultMap deleteCompany(HttpServletRequest request,
			HttpServletResponse response,Integer companyId) {
		
		log.info("In ProjectCompanyAction deleteCompany Begin!");
		
		try{
			if(companyId == null){
				resultMap.setStatus(STATUS.FAILE);
				log.info("In ProjectCompanyAction deleteCompany companyId is null!");
				return resultMap;
			}
			projectCompanyServiceImpl.delProjectCompany(companyId);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("ProjectCompanyAction deleteCompany error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In ProjectCompanyAction deleteCompany end!");	
		
		return resultMap;
	}
}
